package leetcode.力扣竞赛.第333场周赛;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: yan
 * @description: 合并两个二维数组 - 求和法
 * @create: 2023-02-22 13:42
 **/
public class Solution2570 {

    public static void main(String[] args) {
        int[][] arr1 = new int[][]{{1,2},{2,3},{4,5}};
        int[][] arr2 = new int[][]{{1,4},{3,2},{4,1}};
        System.out.println(mergeArrays(arr1, arr2));
    }

    public static int[][] mergeArrays(int[][] nums1, int[][] nums2) {
        int len1 = nums1.length, len2 = nums2.length;
        int p1 = 0, p2 = 0;
        List<List<Integer>> list = new ArrayList<>();
        while (p1 < len1 && p2 < len2) {
            if (nums1[p1][0] == nums2[p2][0]) {
                List<Integer> temp = new ArrayList<>();
                temp.add(nums1[p1][0]);
                temp.add(nums1[p1][1] + nums2[p2][1]);
                list.add(temp);
                p1++; p2++;
            } else if (nums1[p1][0] < nums2[p2][0]) {
                List<Integer> temp = new ArrayList<>();
                temp.add(nums1[p1][0]);
                temp.add(nums1[p1][1]);
                list.add(temp);
                p1++;
            } else {
                List<Integer> temp = new ArrayList<>();
                temp.add(nums2[p2][0]);
                temp.add(nums1[p2][1]);
                list.add(temp);
                p2++;
            }
        }
        if (p1 < len1) {
            for ( ; p1 < len1; p1++){
                List<Integer> temp = new ArrayList<>();
                temp.add(nums1[p1][0]);
                temp.add(nums1[p1][1]);
                list.add(temp);
            }
        }
        if (p2 < len2) {
            for (; p2 < len2; p2++){
                List<Integer> temp = new ArrayList<>();
                temp.add(nums2[p2][0]);
                temp.add(nums2[p2][1]);
                list.add(temp);
            }
        }
        int[][] res = new int[list.size()][2];
        for (int i = 0; i < list.size(); i++) {
            res[i][0] = list.get(i).get(0);
            res[i][1] = list.get(i).get(1);
        }
        return res;
    }

}
